package org.toonan.rental.chart;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class PieChartActivity extends Activity implements AdapterView.OnItemClickListener
{
	private static final float centerX = 369.0F;
	private static final float centerY = 243.17F;
	private static final int len = 5;
	private static final float radius = 120.0F;
	Thread animThread = new Thread()
	{
		public void run()
		{
			int i = 0;
			while (true)
			{
				if (i >= PieChartActivity.this.views.length)
					return;
				Message localMessage = PieChartActivity.this.handler.obtainMessage();
				localMessage.what = 0;
				int j = i + 1;
				localMessage.arg1 = i;
				PieChartActivity.this.handler.sendMessage(localMessage);
				try
				{
					Thread.sleep(500L);
					i = j;
				}
				catch (InterruptedException localInterruptedException)
				{
					localInterruptedException.printStackTrace();
					i = j;
				}
			}
		}
	};
	DataHolder dataHolder;
	private List<Float> endArcs;
	Handler handler = new Handler()
	{
		public void handleMessage(Message paramMessage)
		{
			super.handleMessage(paramMessage);
			switch (paramMessage.what)
			{
			
				case 0:
					PieChartActivity.this.views[(paramMessage.arg1 - 1)].set3D(false);
					PieChartActivity.this.views[(paramMessage.arg1 - 1)].invalidate();
					PieChartActivity.this.add3dView(paramMessage.arg1);
					break;
				default:
					PieChartActivity.this.add3dView(paramMessage.arg1);
					break;
			}
		}
	};
	ListView listView;
	private ViewGroup.LayoutParams params;
	private List<Float> percents;
	PieAdapter pieAdapter;
	private FrameLayout pieViewGroup;
	private int selected = -1;
	private List<Float> startArcs;
	private float sum = 0.0F;
	private int[] viewColors = new int[5];
	private PieChartView[] views = new PieChartView[5];
	
	private void add3dView(int paramInt)
	{
		this.pieViewGroup.removeView(this.views[paramInt]);
		this.views[paramInt].set3D(true);
		PieChartView localPieChartView = this.views[paramInt];
		this.pieViewGroup.addView(localPieChartView, this.params);
	}
	
	private void createPieChartView()
	{
		this.pieViewGroup = ((FrameLayout) findViewById(2131230750));
		this.params = new ViewGroup.LayoutParams(650, 400);
		ArrayList localArrayList = new ArrayList();
		this.startArcs = new ArrayList();
		this.endArcs = new ArrayList();
		this.percents = new ArrayList();
		float f1 = 0.0F;
		float f2 = 0.0F;
		float[] arrayOfFloat = this.dataHolder.values;
		int i = arrayOfFloat.length;
		int j = 0;
		if (j >= i)
			;
		for (int k = this.dataHolder.values.length - 1;; k--)
		{
			if (k <= -1)
			{
				float f3 = arrayOfFloat[j];
				f2 += f1;
				float f4 = f3 / this.sum;
				f1 = f4 * 360.0F;
				float f5 = new BigDecimal(100.0F * f4).setScale(1, 4).floatValue();
				this.percents.add(Float.valueOf(f5));
				localArrayList.add(Float.valueOf(f1));
				this.startArcs.add(Float.valueOf(f2));
				this.endArcs.add(Float.valueOf(f2 + f1));
				j++;
				break;
			}
			this.views[k] = new PieChartView(this, this.dataHolder.strings[k], this.dataHolder.values[k],
					((Float) this.startArcs.get(k)).floatValue(),
					((Float) localArrayList.get(k)).floatValue(), k);
			this.views[k].setId(k);
			this.viewColors[k] = this.views[k].getColor(k);
			this.pieViewGroup.addView(this.views[k], this.params);
		}
	}
	
	private double figureAngle(float paramFloat1, float paramFloat2)
	{
		double d1 = Math.sqrt((243.17F - paramFloat2) * (243.17F - paramFloat2) + (369.0F - paramFloat1)
				* (369.0F - paramFloat1));
		double d4;
		if (d1 > 120.0D)
			d4 = -1.0D;
		double d2 = Math.asin((paramFloat2 - 243.17F) / d1);
		double d3 = 180.0D * d2 / 3.141592653589793D;
		if (paramFloat1 < 369.0F)
			d3 = 180.0D - d3;
		if (d2 < 0.0D)
		{
			double d5 = Math.asin((243.17F - paramFloat2) / d1);
			d3 = 180.0D + 180.0D * d5 / 3.141592653589793D;
			if (paramFloat1 > 369.0F)
				d3 = 360.0D - 180.0D * d5 / 3.141592653589793D;
		}
		d4 = d3;
		return d4;
	}
	
	private void getData()
	{
		Bundle localBundle = getIntent().getBundleExtra("bundle");
		this.dataHolder = new DataHolder();
		String[] arrayOfString = localBundle.getStringArray("strings");
		float[] arrayOfFloat1 = localBundle.getFloatArray("values");
		Float[] arrayOfFloat = new Float[arrayOfFloat1.length];
		int i = 0;
		int n;
		int i1;
		float[] arrayOfFloat2;
		int k;
		if (i >= arrayOfFloat1.length)
		{
			this.dataHolder.stringsListArray.addAll(Arrays.asList(arrayOfString));
			this.dataHolder.stringsListArray.add(4, "其他");
			this.dataHolder.valuesListArray.addAll(Arrays.asList(arrayOfFloat));
			this.dataHolder.valuesListArray.add(4, Float.valueOf(this.dataHolder.values[4]));
			arrayOfFloat2 = this.dataHolder.values;
			int j = arrayOfFloat2.length;
			k = 0;
			i1 = 4;
			n = 0;
			if (arrayOfString.length <= 5)
				
				for (int m = 4;; m++)
				{
					if (m >= arrayOfString.length)
					{
						return;
					}
					
					this.dataHolder.strings[4] = "其他";
					
					arrayOfFloat[i] = Float.valueOf(arrayOfFloat1[i]);
					i++;
					this.dataHolder.strings[n] = arrayOfString[n];
					this.dataHolder.values[n] = arrayOfFloat1[n];
					n++;
					if (n > 4)
						break;
					float[] arrayOfFloat3 = this.dataHolder.values;
					arrayOfFloat3[4] += arrayOfFloat1[i1];
					i1++;
					if (i1 > arrayOfFloat1.length)
						break;
					this.dataHolder.strings = arrayOfString;
					this.dataHolder.values = arrayOfFloat1;
					this.dataHolder.stringsListArray.addAll(Arrays.asList(arrayOfString));
					this.dataHolder.valuesListArray.addAll(Arrays.asList(arrayOfFloat));
					this.sum = (arrayOfFloat2[k] + this.sum);
					k++;
					if (k < j)
						break;
					float f = new BigDecimal(100.0F * (arrayOfFloat1[m] / this.sum)).setScale(1, 4)
							.floatValue();
					this.dataHolder.percent.add(Float.valueOf(f));
				}
		}
		
	}
	
	private void initListView()
	{
		this.listView = ((ListView) findViewById(2131230752));
		this.pieAdapter = new PieAdapter();
		this.listView.setAdapter(this.pieAdapter);
		this.listView.setOnItemClickListener(this);
	}
	
	private void set3Dview(int paramInt)
	{
		PieChartView[] arrayOfPieChartView = this.views;
		int i = arrayOfPieChartView.length;
		for (int j = 0;; j++)
		{
			if (j >= i)
			{
				add3dView(paramInt);
				return;
			}
			PieChartView localPieChartView = arrayOfPieChartView[j];
			localPieChartView.set3D(false);
			localPieChartView.invalidate();
		}
	}
	
	protected void onCreate(Bundle paramBundle)
	{
		super.onCreate(paramBundle);
		setContentView(R.layout.pie_level);
		getData();
		createPieChartView();
		initListView();
		this.animThread.start();
	}
	
	public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong)
	{
		if (paramInt > 4)
			set3Dview(4);
		this.selected = paramInt;
		this.listView.setAdapter(this.pieAdapter);
		this.listView.smoothScrollToPosition(this.selected);
		set3Dview(paramInt);
	}
	
	public boolean onTouchEvent(MotionEvent paramMotionEvent)
	{
		double d;
		int i;
		if (paramMotionEvent.getAction() == 0)
		{
			float f1 = paramMotionEvent.getX();
			float f2 = paramMotionEvent.getY();
			Log.i("tag", f1 + " " + f2);
			d = figureAngle(f1, f2);
			if (-1.0D != d)
			{
				i = 0;
				for (boolean bool = true;; bool = super.onTouchEvent(paramMotionEvent))
				{
					if ((d > ((Float) this.startArcs.get(i)).floatValue())
							&& (d < ((Float) this.endArcs.get(i)).floatValue()))
					{
						set3Dview(i);
						this.selected = i;
						this.listView.setAdapter(this.pieAdapter);
						this.listView.smoothScrollToPosition(this.selected);
					}
					i++;
					if (i < this.dataHolder.values.length)
						break;
				}
			}
		}
		return false;
		
	}
	
	public class DataHolder
	{
		ArrayList<Float> percent = new ArrayList();
		String[] strings = new String[5];
		LinkedList<String> stringsListArray = new LinkedList();
		String title;
		float[] values = new float[5];
		LinkedList<Float> valuesListArray = new LinkedList();
		
		public DataHolder()
		{
		}
	}
	
	private class PieAdapter extends BaseAdapter
	{
		private PieAdapter()
		{
		}
		
		public int getCount()
		{
			return PieChartActivity.this.dataHolder.stringsListArray.size();
		}
		
		public Object getItem(int paramInt)
		{
			return Integer.valueOf(paramInt);
		}
		
		public long getItemId(int paramInt)
		{
			return paramInt;
		}
		
		public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
		{
			View localView;
			Views localViews;
			localView = paramView;
			if (paramView == null)
			{
				localView = LayoutInflater.from(PieChartActivity.this).inflate(R.layout.piechart_item, null);
				localViews = new Views();
				localViews.image = ((ImageView) localView.findViewById(2131230733));
				localViews.text1 = ((TextView) localView.findViewById(2131230721));
				localViews.text2 = ((TextView) localView.findViewById(2131230734));
				localViews.text3 = ((TextView) localView.findViewById(2131230729));
				localView.setTag(localViews);
				if (paramInt >= 5)
				{
					localViews.image.setBackgroundColor(PieChartActivity.this.viewColors[paramInt]);
					localViews.text2.setText(PieChartActivity.this.percents.get(paramInt) + "%");
				}
				
			}
			localViews = (Views) localView.getTag();
			localViews.text1.setText("" + PieChartActivity.this.dataHolder.valuesListArray.get(paramInt));
			localViews.text3.setText((CharSequence) PieChartActivity.this.dataHolder.stringsListArray
					.get(paramInt));
			if (PieChartActivity.this.selected == paramInt)
				localView.setBackgroundResource(R.drawable.item_selection);
			localViews.image.setBackgroundColor(0);
			if (paramInt - 5 >= PieChartActivity.this.dataHolder.percent.size())
				localViews.text2.setText(PieChartActivity.this.dataHolder.percent.get(paramInt - 5) + "%");
			return localView;
		}
		
		class Views
		{
			ImageView image;
			TextView text1;
			TextView text2;
			TextView text3;
			
			Views()
			{
			}
		}
	}
}
